﻿using System;

namespace FlyRemote.Base
{
    public class Singleton<T> where T : class
    {
        private static T _instance;
        private static readonly object _lock = new object();


        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            //_instance = new T();
                            //需要非公共的无参构造函数，不能使用new T() ,new不支持非公共的无参构造函数 
                            _instance = (T)Activator.CreateInstance(typeof(T), true); //第二个参数防止异常：“没有为该对象定义无参数的构造函数。”

                        }
                    }
                }
                return _instance;
            }
        }


    }

}
